package com.lbmb.mdps.settings.controller;

import com.lbmb.mdps.commons.constant.Constant;
import com.lbmb.mdps.commons.domain.ReturnObjectVO;
import com.lbmb.mdps.commons.domain.UserReceive;
import com.lbmb.mdps.commons.exception.AdministratorException;
import com.lbmb.mdps.commons.utils.Md5Util;
import com.lbmb.mdps.settings.domain.Administrator;
import com.lbmb.mdps.settings.service.AdministratorService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 林文塨
 * @date 2022-05-30 1:46
 * description
 */

@CrossOrigin
@RestController
public class AdministratorController {

    @Resource
    private AdministratorService administratorService;

    @PostMapping( "/settings/administrator/login.do")
    public ReturnObjectVO login(@RequestBody Administrator administrator) throws AdministratorException {
        /*
         * 管理员登陆操作
         * @author 林文塨
         * @date 2022/5/30 8:52
         * @param [administrator]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */

        ReturnObjectVO rov = new ReturnObjectVO();
        administrator.setPassword(Md5Util.getMd5(administrator.getPassword()));

        Administrator administratorVo = administratorService.login(administrator);

        administratorVo.setPassword("");

        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
        rov.setOthers(administratorVo);

        return rov;
    }

    @PostMapping( "/settings/administrator/getPrivilege.do")
    public ReturnObjectVO getPrivilege(@RequestBody UserReceive userReceive) throws AdministratorException {
        /*
         * 管理员登陆操作
         * @author 林文塨
         * @date 2022/5/30 8:52
         * @param [administrator]
         * @return com.lbmb.mdps.commons.domain.ReturnObjectVO
         */
        ReturnObjectVO rov = new ReturnObjectVO();

        Boolean isAdministrator = administratorService.getPrivilege(Long.valueOf(userReceive.getCurrentUserId()));

        rov.setOthers(isAdministrator);
        rov.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

        return rov;
    }
}
